package com.predict.javademo.services.impl;

import com.alibaba.fastjson.JSON;
import com.mysql.cj.xdevapi.JsonString;
import com.predict.javademo.mapper.DataCollectMapper;
import com.predict.javademo.result.AdversarialResult;
import com.predict.javademo.result.DefenseResult;
import com.predict.javademo.result.OriginalResult;
import com.predict.javademo.services.DisplayDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DisplayDataServiceImpl implements DisplayDataService {

    @Autowired
    private DataCollectMapper dataCollectMapper;

    @Override
    public ResponseEntity displayData() {
        List<AdversarialResult> adversarialResult = dataCollectMapper.collectAdversarial();
        List<OriginalResult> originalResult = dataCollectMapper.collectOriginal();
        List<DefenseResult> defenseResult = dataCollectMapper.collectDefense();

        if (null == adversarialResult || null == originalResult || null == defenseResult) {
            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }

        return new ResponseEntity<>(JSON.toJSONString(Arrays.asList(Map.of("adversarial", adversarialResult),
                Map.of("original", originalResult),
                Map.of("defense", defenseResult))),
                HttpStatus.OK);
    }
}
